<?php 
if(!defined('DREAMSCAPE')){die('Sorry, but this file cannot be directly viewed.');} 
/*   DB Connection   //-------*/
add_admin_menu(L_ADMIN_DB_SETTINGS,array('<a href="?cat=admin&amp;sub=dbcnx">'.L_MENU_EDIT.'</a>'),'mysql');

if($admin_sub !== 'dbcnx'){return;}
add_breadcrumb(L_DBCNX_TITLE);
add_title(L_DBCNX_TITLE);
ozone_action('admin_page', 'dbcnx_content');

function dbcnx_content(){
global $output, $CONFIG;
if(is_posting(L_BUTTON_EDIT)) {

		$_POST['pass'] = !empty($_POST['pass']) ? $_POST['pass'] : $CONFIG['pass'];
	
		if(@!mysql_connect($_POST['host'],$_POST['user'],$_POST['pass'])){
		$sqlhost 	= $CONFIG['host'];
		$sqluser	= $CONFIG['user'];
		$sqlpass 	= $CONFIG['pass'];
		printOut(FAILURE, L_DB_NEW_CNX_FAILED);
		 } elseif (!mysql_select_db($_POST['db'])){
		 $sqldb = $CONFIG['db'];
		printOut(FAILURE, L_DB_NEW_SELECT_FAILED);
		} else {
		
		$sqlhost 	= $_POST['host'];
		$sqluser	= $_POST['user'];
		$sqlpass 	= $_POST['pass'];
		$sqldb 		= $_POST['db'];
		$sqlprefix 	= $CONFIG['prefix'];
		
		$CONFIG['host']		= $_POST['host'];
		$CONFIG['user']		= $_POST['user'];
		$CONFIG['pass']		= $_POST['pass'];
		$CONFIG['db']		= $_POST['db'];

		
	$fp = fopen(DREAMSCAPEPATH.'/config.php',"w+");	
	$config_to_write =
"<?php
/*
------------------------------------------------------------
Dreamscape Config File
============================================================
*/
//DATABASE VARIABLES
\$CONFIG[\'host\']		= '$sqlhost'; //Database host; usually localhost, or something like mysql.server.com
\$CONFIG['user']		= '$sqluser'; //Database User
\$CONFIG['pass']		= '$sqlpass'; //Database Password
\$CONFIG['db']			= '$sqldb'; //Database Name
\$CONFIG['prefix']		= '$sqlprefix'; //Table Prefix
//Stop editing
\$CONFIG['home']		= dirname(__FILE__);
?>";
$config_to_write = applyOzoneAction('config_file', $config_to_write);
	fwrite($fp,$config_to_write);
	fclose($fp);
		printOut(SUCCESS, L_DB_UPDATE_SUCCESS);
		}
	}
    $link = mysql_connect($CONFIG['host'],$CONFIG['user'],$CONFIG['pass']);
	$db = mysql_select_db($CONFIG['db']);
	if($link){
	printOut(SUCCESS,L_DB_CNX_WORKING);
	} else {
	printOut(FAILURE,L_DB_CNX_BROKEN);
	}
		?>
<?php echo $output;?>
<label for="host"><?php echo L_DB_HOSTNAME ?></label><input name="host" type="text" class="formfields" id="host" value="<?php echo $CONFIG['host']; ?>" size="40" />
<img src="images/help.gif" alt="" width="16" height="16" class="hasHelp" id="dbHost" /><br />
<blockquote class="helpContents" id="dbHostHelp"><h5><?php echo L_DB_HOSTNAME ?></h5></blockquote>
<label for="user"><?php echo L_DB_USERNAME ?></label><input name="user" type="text" class="formfields" id="user" value="<?php echo $CONFIG['user']; ?>" size="40" /><br />
<label for="dbpassword"><?php echo L_DB_PASSWORD ?></label><input name="pass" type="password" class="formfields" id="pass" size="40" />
<br />
<?php echo L_DB_PASSWORD_NOTE ?><br />			
<label for="db"><?php echo L_DB_DATABASE_NAME ?></label><input name="db" type="text" class="formfields" id="db" value="<?php echo $CONFIG['db']; ?>" size="31" />
<img src="images/help.gif" alt="" width="16" height="16" class="hasHelp" id="dataBase" /><br />
<blockquote class="helpContents" id="dataBaseHelp"><h5><?php echo L_DB_DATABASE_NAME ?></h5><?php echo L_DB_DATABASE_NAME_HELP ?></blockquote>

    <?php 
	if(is_writable(DREAMSCAPEPATH.'/config.php')){
	?>                	
<label for="edit"></label>
<input type="submit" name="submit" class="buttons" value="<?php echo L_BUTTON_EDIT ?>" />
<?php } else {
printf(ALERT, L_DB_NEEDS_PERMISSIONS);
}
} ?>
